from django.db import models
from wxchat.models import WxUserInfo


class Investigation(models.Model):
    """问卷"""
    name = models.CharField(verbose_name='问卷名称', max_length=64)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '调查问卷'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Question(models.Model):
    """问卷内容"""
    invest = models.ForeignKey(Investigation, verbose_name='问卷名称', on_delete=models.CASCADE, db_constraint=False)
    name = models.CharField(verbose_name='题干', max_length=256)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '问卷内容'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Options(models.Model):
    """问卷选项"""
    question = models.ForeignKey(Question, verbose_name='题干', on_delete=models.CASCADE, related_name="options", db_constraint=False)
    op_name = models.CharField(verbose_name='选项', max_length=256)
    score = models.IntegerField(verbose_name='分数', default=0)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '问卷选项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.op_name


class Grade(models.Model):
    """企业等级"""
    name = models.CharField(verbose_name='等级名称', max_length=24)
    low_limit_score = models.IntegerField(verbose_name='下限分数', default=0)
    upper_limit_score = models.IntegerField(verbose_name='上限限分数', default=0)

    class Meta:
        verbose_name = '等级标准'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class InvestResult(models.Model):
    """调查结果"""
    openid = models.CharField(verbose_name='微信ID', max_length=120)
    name = models.CharField(verbose_name='姓名', max_length=32, blank=True, null=True)
    invest = models.ForeignKey(Investigation, verbose_name='问卷名称', on_delete=models.CASCADE, db_constraint=False)
    score = models.IntegerField(verbose_name='分数', default=0)
    grade_name = models.CharField(verbose_name='等级', max_length=32, default="", blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '调查结果'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.grade_name

    def save(self, *args, **kwargs):
        if self.name is None:
            user = WxUserInfo.objects.filter(openid=self.openid).first()
            self.name = user.nickname

        grade = Grade.objects.filter(low_limit_score__lt=self.score, upper_limit_score__gte=self.score).first()
        if grade:
            self.grade_name = grade.name
        return super().save(*args, **kwargs)